package br.com.cicero.notasfiscais.listener;

import javax.enterprise.event.Observes;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.inject.Inject;

import org.jboss.seam.faces.event.qualifier.After;
import org.jboss.seam.faces.event.qualifier.RestoreView;

import br.com.cicero.notasfiscais.mb.LoginBean;

public class AutorizadorCDI {
	
	private @Inject FacesContext context;
	private @Inject LoginBean loginBean;
	private @Inject NavigationHandler navigationHandler;
	
	public void autoriza(@Observes @After @RestoreView PhaseEvent event) {
		if ("/login.xhtml".equals(context.getViewRoot().getViewId()))
			return;
		
		if (!loginBean.isLogado()) {
			navigationHandler.handleNavigation(context, null, "login?faces-redirect=true");
			context.renderResponse();
		}
	}

}
